package kr.saeildamil.androidstudy.ch12;

import java.util.ArrayList;

import kr.saeildamil.androidstudy.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ListAddDelMulti extends Activity {
	ArrayList<String> arList;
	ArrayAdapter<String> adapter;
	ListView list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listadddel);

		arList = new ArrayList<String>();
		arList.add("First");
		arList.add("Second");
		arList.add("Third");
		arList.add("Fourth");
		arList.add("Fifth");

		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_multiple_choice, arList);

		list = (ListView) findViewById(R.id.ch12_listadddel_list);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		list.setAdapter(adapter);
		list.setOnItemClickListener(listener);
	}

	AdapterView.OnItemClickListener listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Toast.makeText(ListAddDelMulti.this, arList.get(position),
					Toast.LENGTH_LONG).show();

		}
	};

	public void mOnClick(View v) {
		EditText et = (EditText) findViewById(R.id.ch12_listadddel_edit);
		switch (v.getId()) {
		case R.id.ch12_listadddel_addbtn:
			String str = et.getText().toString();
			et.setText("");
			arList.add(str);
			adapter.notifyDataSetChanged();
			break;
		case R.id.ch12_listadddel_delbtn:
			SparseBooleanArray sbar = list.getCheckedItemPositions();
			Toast.makeText(this, "선택한 사이즈" + sbar.size(), Toast.LENGTH_LONG)
					.show();
			if (sbar.size() != 0) {
				for (int i = list.getCount() - 1; i >= 0; i--) {
					if (sbar.get(i)) {
						arList.remove(i);
					}
				}
			}
			list.clearChoices();
			adapter.notifyDataSetChanged();
			break;
		}
	}
}
